FetchData.fetchData   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
c 0
b 0
f 0
rs 9.95
cc 1
1
import React, { Component, PropTypes } from 'react';
2
import { bindActionCreators } from 'redux';
3
import { connect } from 'react-redux';
4
import { RouterContext } from 'react-router';
5
import { doneFetching } from './module';
6
import { grabPromises } from './utils';
7
8
export class FetchData extends Component {
9
  componentWillMount() {
10
    if (!this.props.isFetched) {
11
      this.fetchData(this.props);
12
    }
13
  }
14
15
  componentWillReceiveProps(nextProps) {
16
    this.fetchData(nextProps);
17
  }
18
19
  fetchData(props) {
20
    const promises = grabPromises(
21
      props.components,
22
      props.params,
23
      this.context.store
24
    );
25
26
    Promise.all(promises).then(() => {
27
      this.props.actions.doneFetching();
28
    });
29
  }
30
31
  render() {
32
    return <RouterContext {...this.props}/>;
33
  }
34
}
35
36
FetchData.propTypes = {
37
  isFetched: PropTypes.bool.isRequired,
38
  actions: PropTypes.object
39
};
40
41
FetchData.defaultProps = {
42
  isFetched: false
43
};
44
45
FetchData.contextTypes = {
46
  store: PropTypes.object.isRequired
47
};
48
49
function mapStateToProps(state) {
50
  return {
51
    isFetched: state.fetching.fetched
52
  };
53
}
54
55
function mapDispatchToProps(dispatch) {
56
  return {
57
    actions: bindActionCreators({ doneFetching }, dispatch)
58
  };
59
}
60
61
export default connect(mapStateToProps, mapDispatchToProps)(FetchData);
62